import { mongo } from "../mogoConfig";

type Minutes = number;

interface IMDB {
    rating: number;
    votes: number;
    id: number;
}

interface Movie {
    title: string;
    imdb: IMDB;
    runtime: Minutes;
}

const db = mongo.db("sample_mflix")
const movies = db.collection<Movie>("movies")
const cursor = movies.find<Movie>({ runtime: { $lt: 15 } },
    {
        sort: { title: 1 },
        projection: { _id: 0, title: 1, imdb: 1 }
    }
)

const run = async () => {
    for await (const movie of cursor) {
        console.log(JSON.stringify(movie))
    }
}

movies.countDocuments({ runtime: { $lt: 15 } })
    .then(result => {
        console.log("数量是:", result)
        return cursor
    })
    .then(() => run())
    .finally(() => {
        mongo.close()
    })


console.log("执行结束")